﻿using System;
using System.Xml.Serialization;

namespace WhiteFlow.DevTrackingTool.UI.Models
{
    /// <summary>
    /// Contains settings for a tracking worksheet.
    /// </summary>
    [Serializable]
    public class WorksheetSettings : WorksheetItem
    {
        #region Constants

        /// <summary>
        /// The ContractWorkdayDuration property name.
        /// </summary>
        public const string ContractWorkdayDurationPropertyName = "ContractWorkdayDuration";

        /// <summary>
        /// The Currency property name.
        /// </summary>
        public const string CurrencyPropertyName = "Currency";

        /// <summary>
        /// The RefundPerKilometer property name.
        /// </summary>
        public const string RefundPerKilometerPropertyName = "RefundPerKilometer";

        #endregion

        #region Fields

        private TrackingWorksheet owner;

        private TimeSpan contractWorkdayDuration;

        private string currency;

        private decimal refundPerKilometer;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the duration of the workday (in terms of contract).
        /// </summary>
        /// <value>
        /// The duration of the workday (expressed as a <see cref="TimeSpan"/> instance).
        /// </value>
        [XmlIgnore]
        public TimeSpan ContractWorkdayDuration
        {
            get { return contractWorkdayDuration; }
            set
            {
                contractWorkdayDuration = value;
                OnPropertyChanged(ContractWorkdayDurationPropertyName);

                if (owner != null)
                {
                    owner.UpdateOwners();
                }
            }
        }

        /// <summary>
        /// Gets or sets the duration of the workday (in terms of contract).
        /// </summary>
        /// <value>
        /// The duration of the workday (expressed as ticks).
        /// </value>
        [XmlElement(ElementName = ContractWorkdayDurationPropertyName, IsNullable = false)]
        public long ContractWorkdayDurationTicks
        {
            get { return ContractWorkdayDuration.Ticks; }
            set { ContractWorkdayDuration = new TimeSpan(value); }
        }

        [XmlElement(ElementName = RefundPerKilometerPropertyName, IsNullable = false)]
        public decimal RefundPerKilometer
        {
            get { return refundPerKilometer; }
            set
            {
                refundPerKilometer = value;
                OnPropertyChanged(RefundPerKilometerPropertyName);

                if (owner != null)
                {
                    owner.UpdateOwners();
                }
            }
        }

        [XmlElement(ElementName = CurrencyPropertyName, IsNullable = false)]
        public string Currency
        {
            get
            {
                if (currency == null)
                    currency = "en-US";

                return currency;
            }
            set
            {
                currency = value;
                OnPropertyChanged(CurrencyPropertyName);
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WorksheetSettings"/> class.
        /// </summary>
        public WorksheetSettings()
        {
            // Populate default values
            ContractWorkdayDuration = new TimeSpan(8, 0, 0);
        }

        #endregion

        #region Public Methods

        public void SetOwner(TrackingWorksheet owner)
        {
            this.owner = owner;

            if (owner != null)
            {
                owner.UpdateOwners();
            }
        }

        #endregion
    }
}